package com.smallA.association.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class TestController {

    //请求订单列表
    @RequestMapping(value = "/Test/upload", method = RequestMethod.POST)
    public String getOrderDtl(@RequestParam(value = "file") MultipartFile files[]) {
        System.out.println("开始操作图片上传");
        //String uploadPath = "C:\\Users\\wanda\\Desktop\\test";
        String uploadPath = "/home/JavaProject/ImgFile";
        File uploadDirectory = new File(uploadPath);
        if (uploadDirectory.exists()) {
            if (!uploadDirectory.isDirectory()) {
                // 这里是判断是否为一个目录
                return null;
            }
        } else {
            uploadDirectory.mkdir();
        }
        // 这里可以支持多文件上传
        if (files != null && files.length >= 1) {
            BufferedOutputStream bw = null;
            try {
                for (MultipartFile file : files) {
                    String fileName = file.getOriginalFilename();
                    //判断是否有文件且是否为图片文件
                    if (fileName != null && !"".equalsIgnoreCase(fileName.trim()) && isImageFile(fileName)
                    ) {
                        //创建输出文件对象
                        File outFile = new File(uploadPath + "/" + UUID.randomUUID().toString() + getFileType(fileName));
                        System.out.println(outFile.getPath());
                        //拷贝文件到输出文件对象
                        System.out.println("遍历到一个文件");
                        FileUtils.copyInputStreamToFile(file.getInputStream(), outFile);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bw != null) {
                        bw.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "upload successful";
    }

    private Boolean isImageFile(String fileName) {
        String[] img_type = new String[]{".jpg", ".jpeg", ".png", ".gif", ".bmp"};
        if (fileName == null) {
            return false;
        }
        fileName = fileName.toLowerCase();
        for (String type : img_type) {
            if (fileName.endsWith(type)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取文件后缀名
     *
     * @param fileName
     * @return
     */
    private String getFileType(String fileName) {
        if (fileName != null && fileName.indexOf(".") >= 0) {
            return fileName.substring(fileName.lastIndexOf("."), fileName.length());
        }
        return "";
    }

}
